﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;

namespace WQDSkin.Forms.Controls
{
    public class WQDToggleButtonAutomationPeer : ButtonBaseAutomationPeer, IToggleProvider
    {
        ///
        public WQDToggleButtonAutomationPeer(WQDToggleButton owner) : base(owner)
        { }

        ///
        override protected string GetClassNameCore()
        {
            return "Button";
        }

        ///
        override protected AutomationControlType GetAutomationControlTypeCore()
        {
            return AutomationControlType.Button;
        }

        /// 
        override public object GetPattern(PatternInterface patternInterface)
        {
            if (patternInterface == PatternInterface.Toggle)
            {
                return this;
            }
            else
            {
                return base.GetPattern(patternInterface);
            }
        }

        void IToggleProvider.Toggle()
        {
            if (!IsEnabled())
                throw new ElementNotEnabledException();

            WQDToggleButton owner = (WQDToggleButton)Owner;
            owner.OnToggle();
        }

        ToggleState IToggleProvider.ToggleState
        {
            get
            {
                WQDToggleButton owner = (WQDToggleButton)Owner;
                return ConvertToToggleState(owner.IsChecked);
            }
        }
         
        [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
        internal virtual void RaiseToggleStatePropertyChangedEvent(bool? oldValue, bool? newValue)
        {
            if (oldValue != newValue)
            {
                RaisePropertyChangedEvent(TogglePatternIdentifiers.ToggleStateProperty, ConvertToToggleState(oldValue), ConvertToToggleState(newValue));
            }
        }

        private static ToggleState ConvertToToggleState(bool? value)
        {
            switch (value)
            {
                case (true): return ToggleState.On;
                case (false): return ToggleState.Off;
                default: return ToggleState.Indeterminate;
            }
        }
    }
}
